//
//  LLUpDownArrowView.m
//  TaoTuo
//
//  Created by admin on 2019/8/20.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLUpDownArrowView.h"

@interface LLUpDownArrowView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, copy) NSString *titleString;
@property (nonatomic, copy) SelectTypeBlock typeBlock;
@property (nonatomic, assign) BOOL selected;

@property (nonatomic, assign) int b;

@end

@implementation LLUpDownArrowView


+ (instancetype)arrowViewWithFrame:(CGRect)frame title:(NSString *)title selectTypeBlock:(nonnull SelectTypeBlock)selectTypeBlock
{
    return [[self alloc] initWithFrame:frame title:title selectTypeBlock:selectTypeBlock];
}


- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title selectTypeBlock:(nonnull SelectTypeBlock)selectTypeBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.titleString = title;
        self.typeBlock = selectTypeBlock;
        
        [self setupSubviews];
        
        
        @weakify(self)
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self)
            self.b++;
            if (self.b > 2) self.b = 1;
            
            if (self.b == 1) {
                self.arrowImageView.image = [UIImage imageNamed:@"updown_downsel"];
            } else if (self.b == 2) {
                self.arrowImageView.image = [UIImage imageNamed:@"updown_upsel"];
            }
            self.titleLabel.textColor = kMainColor;
            
            if (self.typeBlock) {
                self.typeBlock(self, self.titleLabel.text, self.b - 1);
            }
            
        }];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}


- (void)setSelected:(BOOL)selected arrowType:(ArrowType)arrowType
{
    _selected = selected;
    if (!_selected) {
        [self resetView];
        self.b = 0;
    } else {
        self.b = arrowType + 1;
        self.titleLabel.textColor = kMainColor;
        if (self.b == 1) {
            self.arrowImageView.image = [UIImage imageNamed:@"updown_downsel"];
        } else if (self.b == 2) {
            self.arrowImageView.image = [UIImage imageNamed:@"updown_upsel"];
        }
        if (self.typeBlock) {
            self.typeBlock(self, self.titleLabel.text, self.b - 1);
        }
    }
}


- (void)resetView
{
    self.titleLabel.textColor = UIColorHex(333333);
    self.arrowImageView.image = [UIImage imageNamed:@"updown_unsel"];
}

- (void)setupSubviews
{
    CGFloat w = [self.titleString boundingRectWithSize:CGSizeMake(MAXFLOAT, self.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.titleLabel.font} context:nil].size.width + 5;
    self.titleLabel.frame = CGRectMake(0, 0, w, self.height);
    self.arrowImageView.frame = CGRectMake(self.titleLabel.right, 0, 20, self.height);
    
    self.width = self.titleLabel.width + self.arrowImageView.width;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:self.titleString color:UIColorHex(333333) font:[UIFont boldSystemFontOfSize:14]];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIImageView *)arrowImageView
{
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] init];
        _arrowImageView.image = [UIImage imageNamed:@"updown_unsel"];
        _arrowImageView.contentMode = UIViewContentModeCenter;
        [self addSubview:_arrowImageView];
    }
    return _arrowImageView;
}

@end
